package rickyGame.ui
{
	import comp.Button;
	import comp.InputText;
	import comp.Label;
	import comp.List;
	import comp.TextArea;
	
	import flash.display.DisplayObject;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import rickyGame.game.GameState;
	import rickyGame.model.User;
	

	/**
	 * 大厅
	 */
	public class Hall extends BaseView
	{
		public function Hall()
		{
			super();
			this.width=1000;
			this.height=700;
			this.useTheme=false;
			this.name="ui_hall";
			this.closeClassName="";

		}
		
		
		//override
		override protected function createChildren():void
		{
			_textArea=new TextArea();
			_textArea.htmlText=CLocale.getValue("hallInfo");
			_textArea.width=200;	
			_textArea.height=100;
			_textArea.x=100;
			_textArea.y=100;
			
			this.addChild(_textArea);
			
			_list=new List();
			_list.onItemBind=onItemBind;
			_list.x=400;
			_list.y=100;
			this.addChild(_list);
			
			_input=new InputText();
			_input.x=_textArea.x;
			_input.y=_textArea.y+110;
			_input.text="房间地图："+int(Math.random()*1000);
			this.addChild(_input);
			
			_btn=new Button();
			_btn.text="创建";
			_btn.x=_input.x;
			_btn.y=_input.y+25;
			_btn.clickFunction=create;
			this.addChild(_btn);
			
			var _btn1:Button=new Button();
			_btn1.text="获取列表";
			_btn1.x=_input.x;
			_btn1.y=_input.y+50;
			_btn1.clickFunction=getList;
			this.addChild(_btn1);
			
			
			_btn1=new Button();
			_btn1.text="加入地图";
			_btn1.x=_input.x;
			_btn1.y=_input.y+75;
			_btn1.clickFunction=joinMap;
			this.addChild(_btn1);
			
			
			_btn1=new Button();
			_btn1.text="开始游戏";
			_btn1.x=_input.x;
			_btn1.y=_input.y+100;
			_btn1.clickFunction=begin;
			this.addChild(_btn1);
			
		}
		
		/**
		 * 进入游戏
		 */
		private function begin(e:MouseEvent):void
		{
//			trace(User.currRoom.createName);
//			trace(User.heroName);
			if(User.currRoom.createName==User.userName)
			{
				var msg:Object=new Object();
				msg["cmd"]="Room";
				msg["t"]=2;//开始加载开始加载
				MsgHelper.sendMsgToS(msg);
			}
		}
		
		private function getList(e:Event):void
		{
			var msg:Object=new Object();
			msg["cmd"]="Pub";
			msg["t"]=10;//得到房间列表
			MsgHelper.sendMsgToS(msg);
		}
		
		private function joinMap(e:MouseEvent):void
		{
			
			var mapInfo:Object=_list.selectedValue;
			if(mapInfo)
			{
				var msg:Object=new Object();
				msg["cmd"]="Room";
				msg["t"]=1;// 加入房间
				msg["roomID"]=mapInfo.id;
				MsgHelper.sendMsgToS(msg);
			}
		}
		
		private function create(e:Event):void
		{
			var msg:Object=new Object();
			msg["cmd"]="Room";
			msg["t"]=0;// 创建房间
			msg["name"]=_input.text;
			msg["info"]=new Object();
			msg["info"].name=_input.text;
			msg["info"].url="config/scene/haiwai.map";//testScene01.zf3d
			MsgHelper.sendMsgToS(msg);
		}
		
		private function onItemBind(i:int,data:Object):DisplayObject
		{
			var label:Button=new Button();
			label.text=data["name"];
			label.tag=i;
			label.clickFunction=chooseMap;
			return label;
		}
		
		private function chooseMap(e:MouseEvent):void
		{
			if(_list.lastSelectedItem)
				_list.lastSelectedItem.selected=false;
			_list.selectedIndex=e.target.tag;
			_list.selectedItem.selected=true;
		}
		
		/**
		 * 显示房间列表
		 */
		public function showList(list:Array):void
		{
			_list.data=list;
		}
		
		private var _textArea:TextArea;
		private var _list:List;
		private var _input:InputText;
		private var _btn:Button;
	}
}